package com.nbsaas.boot.evaluate.rest.resource;

import com.nbsaas.boot.evaluate.api.apis.EvaluateReplyApi;
import com.nbsaas.boot.entity.evaluate.EvaluateReply;
import com.nbsaas.boot.evaluate.api.domain.request.EvaluateReplyRequest;
import com.nbsaas.boot.evaluate.api.domain.response.EvaluateReplyResponse;
import com.nbsaas.boot.evaluate.api.domain.simple.EvaluateReplySimple;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateReplySimpleConvert;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateReplyEntityConvert;
import com.nbsaas.boot.evaluate.rest.convert.EvaluateReplyResponseConvert;
import com.nbsaas.boot.evaluate.data.repository.EvaluateReplyRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.enums.StoreState;
import com.nbsaas.boot.rest.request.RequestId;
import java.util.Optional;
import com.nbsaas.boot.rest.response.ResponseObject;
/**
*   评价回复-业务接口实现
*/
@Transactional
@Service
public class EvaluateReplyResource extends BaseResource<EvaluateReply,EvaluateReplyResponse, EvaluateReplySimple, EvaluateReplyRequest>  implements EvaluateReplyApi {

    @Resource
    private EvaluateReplyRepository evaluateReplyRepository;

    @Override
    public JpaRepositoryImplementation<EvaluateReply, Serializable> getJpaRepository() {
        return evaluateReplyRepository;
    }

    @Override
    public Function<EvaluateReply, EvaluateReplySimple> getConvertSimple() {
        return new EvaluateReplySimpleConvert();
    }

    @Override
    public Function<EvaluateReplyRequest, EvaluateReply> getConvertForm() {
        return new EvaluateReplyEntityConvert();
    }

    @Override
    public Function<EvaluateReply, EvaluateReplyResponse> getConvertResponse() {
        return new EvaluateReplyResponseConvert();
    }




    @Override
    public ResponseObject<EvaluateReplyResponse> create(EvaluateReplyRequest request) {
        request.setStoreState(StoreState.normal);
        return super.create(request);
   }

    @Override
    public ResponseObject<?> delete(RequestId request) {

        ResponseObject<?> result = new ResponseObject<>();
        Optional<EvaluateReply> optional = getJpaRepository().findById(request.getId());
        if (!optional.isPresent()) {
            result.setCode(501);
            result.setMsg("无效id");
            return result;
        }
        EvaluateReply bean = optional.get();
        bean.setStoreState(StoreState.draft);
        return result;

    }
}


